package com.hjm.project.recruitTeam.recruitTeam.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hjm.common.exception.BusinessException;
import com.hjm.common.utils.DateUtils;
import com.hjm.common.utils.StringUtils;
import com.hjm.common.utils.fastdfs.FastDFSClient;
import com.hjm.common.utils.security.ShiroUtils;
import com.hjm.framework.config.CustomConfig;
import com.hjm.project.agentInfo.agentInfo.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hjm.project.recruitTeam.recruitTeam.mapper.RecruitTeamMapper;
import com.hjm.project.recruitTeam.recruitTeam.domain.RecruitTeam;
import com.hjm.project.recruitTeam.recruitTeam.service.IRecruitTeamService;
import com.hjm.common.utils.text.Convert;
import org.springframework.web.multipart.MultipartFile;

/**
 * 招募团队文案Service业务层处理
 * 
 * @author LOVE
 * @date 2020-11-26
 */
@Service
public class RecruitTeamServiceImpl implements IRecruitTeamService 
{
    @Autowired
    private RecruitTeamMapper recruitTeamMapper;

    /**
     * 查询招募团队文案
     * 
     * @param id 招募团队文案ID
     * @return 招募团队文案
     */
    @Override
    public RecruitTeam selectRecruitTeamById(Long id)
    {
        return recruitTeamMapper.selectRecruitTeamById(id);
    }

    /**
     * 查询招募团队文案列表
     * 
     * @param recruitTeam 招募团队文案
     * @return 招募团队文案
     */
    @Override
    public List<RecruitTeam> selectRecruitTeamList(RecruitTeam recruitTeam) {
        List<RecruitTeam> recruitTeams = recruitTeamMapper.selectRecruitTeamList(recruitTeam);
        for (RecruitTeam team : recruitTeams) {
            team.setImgUrl(CustomConfig.getImgUrl() + team.getImgUrl());
        }
        return recruitTeams;
    }

    /**
     * 新增招募团队文案
     * 
     * @param recruitTeam 招募团队文案
     * @return 结果
     */
    @Override
    public int insertRecruitTeam(MultipartFile imgUrl, RecruitTeam recruitTeam)throws Exception
    {
        //判断格式是不是图片格式jpg png gif
        if (imgUrl!=null){
            String fileName =  imgUrl.getOriginalFilename();
            if (fileName.endsWith(".jpg")||fileName.endsWith(".png")||fileName.endsWith(".gif")||fileName.endsWith(".GIF")||fileName.endsWith(".JPG")||fileName.endsWith(".PNG")) {
                //上传文件
                String urlName = uploadImg(imgUrl);
                recruitTeam.setImgUrl(urlName);
            }else{
                throw  new BusinessException("图片上传失败,请重新上传");
            }
        }else{
            throw  new BusinessException("图片上传失败,未选择图片");
        }
        recruitTeam.setCreateTime(DateUtils.getNowDate());
        recruitTeam.setAdminId(Long.valueOf(ShiroUtils.getSysUser().getObjNo()));
        recruitTeam.setRealName(ShiroUtils.getSysUser().getLoginName());
        return recruitTeamMapper.insertRecruitTeam(recruitTeam);
    }

    /**
     * 修改招募团队文案
     * 
     * @param recruitTeam 招募团队文案
     * @return 结果
     */
    @Override
    public int updateRecruitTeam(MultipartFile imgUrl, RecruitTeam recruitTeam) throws Exception
    {
        if (imgUrl!=null){
            String fileName =  imgUrl.getOriginalFilename();
            if (fileName.endsWith(".jpg")||fileName.endsWith(".png")||fileName.endsWith(".gif")||fileName.endsWith(".GIF")||fileName.endsWith(".JPG")||fileName.endsWith(".PNG")) {
                //上传文件
                String urlName = uploadImg(imgUrl);
                recruitTeam.setImgUrl(urlName);
            }else{
                throw  new BusinessException("图片上传失败,请重新上传");
            }
        }
        if (recruitTeam.getMsg() != null){
            String msg = recruitTeam.getMsg().replaceAll("undefined,", "");
            recruitTeam.setMsg(msg);
        }
        recruitTeam.setAdminId(Long.valueOf(ShiroUtils.getSysUser().getObjNo()));
        recruitTeam.setRealName(ShiroUtils.getSysUser().getLoginName());
        return recruitTeamMapper.updateRecruitTeam(recruitTeam);
    }

    /**
     * 删除招募团队文案对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteRecruitTeamByIds(String ids)
    {
        return recruitTeamMapper.deleteRecruitTeamByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除招募团队文案信息
     * 
     * @param id 招募团队文案ID
     * @return 结果
     */
    @Override
    public int deleteRecruitTeamById(Long id)
    {
        return recruitTeamMapper.deleteRecruitTeamById(id);
    }


    public String uploadImg(MultipartFile imgFile) throws IOException {
        String clientName = imgFile.getOriginalFilename();
        if (StringUtils.isEmpty(clientName)) {
            return null;
        }
        String fileName = null;
        fileName = clientName.substring(clientName.indexOf("."), clientName.length());
        //临时文件
        File dfile = File.createTempFile("prefix", "_" + clientName);
        imgFile.transferTo(dfile);
        //上传至Fast的图片数据
        Map<String, String> metaList = new HashMap<String, String>();
        metaList.put("fileName", fileName);
        metaList.put("fileType", imgFile.getName());
        metaList.put("author", ShiroUtils.getSysUser().getLoginName());
        metaList.put("date", Utils.formateDate(2));//年月日 时分秒
        String fid = FastDFSClient.uploadFile(dfile, dfile.getName(), metaList);
        if (StringUtils.isEmpty(fid)) {
            throw  new BusinessException("图片上传失败,请重新上传");
        }
        return fid;
    }
}
